package com.wei.shopping.controller;

import com.tencentcloudapi.cdn.v20180606.models.Https;
import com.wei.shopping.entrty.PageResult;
import com.wei.shopping.entrty.QueryPageBean;
import com.wei.shopping.entrty.Result;
import com.wei.shopping.pojo.Commodity;
import com.wei.shopping.pojo.Login;
import com.wei.shopping.pojo.Sold;
import com.wei.shopping.service.CommService;
import com.wei.shopping.service.LoginService;
import com.wei.shopping.service.SoldService;
import com.wei.shopping.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/sold")
public class SoldController {
    @Resource
    private SoldService soldService;
    @Resource
    private UserService userService;
    @Resource
    private LoginService loginService;
    @Resource
    private CommService commService;

    @ResponseBody
    @RequestMapping("/findall")
    public PageResult findall(@RequestBody QueryPageBean queryPageBean,String findString){
        try{
            PageResult page = soldService.findall(queryPageBean, findString);
            return page;
        }catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping("/findAllMessage")
    @ResponseBody
    public Result console(){
        Map map = new HashMap();
        //获取月份列表
        //获取当日日期之前12个月的日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.MONTH,-12);
        List<String> months = new ArrayList<>();
        for(int i = 0 ;i < 12 ;i++){
            calendar.add(calendar.MONTH,1);
            months.add(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
        }
        List<Integer> girl = new ArrayList<Integer>();
        girl = soldService.findGroupBySex(months,"女");
        map.put("girl",girl);
        List<Integer> boy = new ArrayList<Integer>();
        boy = soldService.findGroupBySex(months,"男");
        map.put("boy",boy);
        List<Integer> count = new ArrayList<>();
        for (Integer g : girl) {
            for (Integer b : boy) {
                count.add(g+b);
            }
        }
        map.put("count",count);
        return new Result(true,"查找成功",map);
    }

    /**
     * 查找已售商品
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("/findallbyuserid")
    public PageResult findallbyuserid(HttpSession session,@RequestBody QueryPageBean queryPageBean){
        String userid = (String) session.getAttribute("userid");
        try{
            PageResult findallbyuserid = soldService.findallbyuserid(userid, queryPageBean);
            return findallbyuserid;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 删除销售记录
     * @param soldid
     * @return
     */
    @ResponseBody
    @RequestMapping("/deletecomm")
    public Result deletecomm(@RequestParam("soldid") String soldid){
        try{
            soldService.deletecomm(soldid);
            return new Result(true,"'删除记录成功");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"删除记录失败");
        }
    }

    /**
     * 每个月的销售总量
     * @param session
     * @return
     */
    @RequestMapping("/findCountByUserId")
    @ResponseBody
    public Result findCountByUserId(HttpSession session){
        String userid = (String)session.getAttribute("userid");
        //获取月份列表
        //获取当日日期之前12个月的日期
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.MONTH,-12);
        List<String> months = new ArrayList<>();
        for(int i = 0 ;i < 12 ;i++){
            calendar.add(calendar.MONTH,1);
            months.add(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
        }
        List<Integer> count = soldService.findAccount(months,userid);
        return new Result(true,"查找成功",count);
    }

    @RequestMapping("/findorder")
    @ResponseBody
    public PageResult findorder(@RequestBody QueryPageBean queryPageBean,HttpSession session){
        String userid = (String)session.getAttribute("userid");
        try{
            PageResult findorder = soldService.findorder(queryPageBean,userid);
            return findorder;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
